package com.ruoyi.apiserver.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.apiserver.utils.JwtUtils;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.members.domain.MhUser;
import com.ruoyi.members.service.IMhUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 用户相关接口
 * 
 * @author ruoyi
 */
@Api(tags = "用户相关接口")
@RestController
@RequestMapping("/api/user")
public class UserController
{
    @Autowired
    private IMhUserService userService;
    
    @Autowired
    private JwtUtils jwtUtils;

    /**
     * 用户登录
     */
    @ApiOperation("用户登录")
    @Anonymous
    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginBody loginBody)
    {
        // 查询用户信息
        MhUser user = new MhUser();
        user.setPhone(loginBody.getPhone());
        user.setPassword(loginBody.getPassword());
        
        // 简单验证，实际项目中需要加密密码
        // 这里为了演示，假设验证通过
        MhUser loginUser = userService.selectMhUserList(user).stream().findFirst().orElse(null);
        
        if (loginUser == null)
        {
            return AjaxResult.error("手机号或密码错误");
        }
        
        // 生成token
        String token = jwtUtils.generateToken(loginUser.getPhone());
        
        Map<String, Object> result = new HashMap<>();
        result.put("token", token);
        result.put("user", loginUser);
        
        return AjaxResult.success("登录成功", result);
    }

    /**
     * 用户注册
     */
    @ApiOperation("用户注册")
    @Anonymous
    @PostMapping("/register")
    public AjaxResult register(@RequestBody RegisterBody registerBody)
    {
        // 检查手机号是否已注册
        MhUser user = new MhUser();
        user.setPhone(registerBody.getPhone());
        if (!userService.selectMhUserList(user).isEmpty())
        {
            return AjaxResult.error("该手机号已注册");
        }
        
        // 创建新用户
        MhUser newUser = new MhUser();
        newUser.setPhone(registerBody.getPhone());
        newUser.setPassword(registerBody.getPassword());
        newUser.setNickname(registerBody.getNickname());
        newUser.setStatus(1L); // 正常状态
        
        int result = userService.insertMhUser(newUser);
        if (result > 0)
        {
            return AjaxResult.success("注册成功");
        }
        else
        {
            return AjaxResult.error("注册失败");
        }
    }

    /**
     * 获取用户信息
     */
    @ApiOperation("获取用户信息")
    @GetMapping("/info")
    public AjaxResult getUserInfo(HttpServletRequest request)
    {
        // 从token中获取手机号
        String token = jwtUtils.getToken(request);
        String phone = jwtUtils.getUsernameFromToken(token);
        
        // 查询用户信息
        MhUser user = new MhUser();
        user.setPhone(phone);
        MhUser userInfo = userService.selectMhUserList(user).stream().findFirst().orElse(null);
        
        if (userInfo == null)
        {
            return AjaxResult.error("用户不存在");
        }
        
        return AjaxResult.success(userInfo);
    }
    

    
    /**
     * 登录请求体
     */
    public static class LoginBody
    {
        private String phone;
        private String password;
        
        public String getPhone()
        {
            return phone;
        }
        
        public void setPhone(String phone)
        {
            this.phone = phone;
        }
        
        public String getPassword()
        {
            return password;
        }
        
        public void setPassword(String password)
        {
            this.password = password;
        }
    }
    
    /**
     * 注册请求体
     */
    public static class RegisterBody
    {
        private String phone;
        private String password;
        private String nickname;
        
        public String getPhone()
        {
            return phone;
        }
        
        public void setPhone(String phone)
        {
            this.phone = phone;
        }
        
        public String getPassword()
        {
            return password;
        }
        
        public void setPassword(String password)
        {
            this.password = password;
        }
        
        public String getNickname()
        {
            return nickname;
        }
        
        public void setNickname(String nickname)
        {
            this.nickname = nickname;
        }
    }

}